// Template for Project 2 for CS3451
// Jarek Rossignac, September 2011
// Tyler Powell, Sandeep Segireddy
import processing.opengl.PGraphicsOpenGL;

import ddf.minim.*; //songStuff
Minim minim; //songStuff
AudioPlayer song; //songStuff

PImage hisFace;
void setup() {               // executed once at the begining 


   minim = new Minim(this);   //songStuff
   song = minim.loadFile("data/MyTango.mp3");//songStuff
   

  size(800, 600, OPENGL);            // window size
  myFace = loadImage("data/pic.jpg");  // load image from file pic.jpg in folder data
  hisFace = loadImage("data/pic2.jpg");
  anchorX=width/2; 
  anchorY=height/2;   // init position for text
  sphereDetail(12); // *3D*
  //  smooth();   strokeJoin(ROUND); strokeCap(ROUND); // for drawing thick curves nicely
  MYsetup();  // execute application specific setup tasks, which are in MYaction
  calcFrames();  //initial calculation of keys for dancer
}

Boolean scribeText=true;  // to toggle displaying help text

String title ="CS3451 Fall 2011, Project 2: Dancer", name ="Tyler Powell / Sandeep Segireddy", 
menu="H:help, X:pict, R:record(on/off), filmUI(S:start, P:pause, E:end), T:text, Q:quit"; // help info

void draw() {      // executed at each frame
  background(255); // clear screen and paints white background
  if (scribeText && !recording&&!filming&&!snapping) {
    scribeFooter(menu); 
    MYmenu ();
  } // writes help 
  if (scribeText && (!filming ||frameCounter==0) ) {
    image(myFace, width-myFace.width/2 - hisFace.width/2 - 10, 25, myFace.width/2, myFace.height/2);
    image(hisFace,width-hisFace.width/2 -5, 25, hisFace.width/2, hisFace.height/2); 
    scribeHeader(title); 
    scribeHeaderRight(name);
  }

  MYdraw();  // execute application specific tasks to draw next frame: in MYaction 

  if (clipPinned) scribeAt(clipText, clipX, clipY); 
  else scribeAtMouse(clipText);  // to write text from clipboard at mouse

  showRubberBand(); // shows line from mouse current position to where it has been pressed
  showAction(); // show mouse and key presses

  if (recording) {
    fill(255, 0, 0); 
    ellipse(width-6, height-6, 6, 6); 
    saveFrame("FRAMES/F"+nf(animationFrameCounter++, 3)+".tif");
  } 
  noFill();
  if (keyPressed&&!captured&&(frameCount-lastFrameCaptured>10)) {
    if (!justCaptured) captureFrame();
  }  // add frame to film if a key remains pressed
  if (snapping) snapPicture();
  justCaptured=false;
}

// Interrupts that handle user actions, with redirections to MYaction folder
void mousePressed() {
  captureFrame(); 
  anchorX=mouseX; 
  anchorY=mouseY; 
  MYmousePressed();
}
void mouseDragged() {
  if (!justCaptured && d(Mouse(), lastCapturedMouse)>3) captureFrame(); 
  MYmouseDragged();
}
void mouseMoved() {
  if (!justCaptured && d(Mouse(), lastCapturedMouse)>3) captureFrame(); 
  MYmouseMoved();
}
void mouseReleased() {
  captureFrame(); 
  MYmouseReleased();
}
void keyReleased() {
  if (key=='T') {
    clipPinned=true; 
    clipX=mouseX; 
    clipY=mouseY;
  } 
  captureFrame(); 
  MYkeyReleased();
}
void keyPressed() {
  
  captured=false;
  captureFrame(); // to film key presses
  if (key == 'T') {
    clipText=getClipboard(); 
    clipPinned=false;
  } // gets text from clipboard
  if (key == 'E') {
    filming=false; 
    startedFilm=false;  
    mm.finish(); // end film. the files capture0x.mov can be exported to a web mp4 format
    println("saved movie: film"+nf(movieCounter, 2)+".mov"+" of "+frameCounter+" frames");
  }
  if (key == 'X') {
    snapping=true;
  } // saves picture of screen
  if (key == 'Q') exit();  // quit application
  if (key == 'S') { 
    movieCounter++; 
    String S = "FILMS/film"+nf(movieCounter, 2)+".mov";   // start new film
    mm = new MovieMaker(this, width, height, S, 15, MovieMaker.JPEG, MovieMaker.LOW); 
    startedFilm=true; 
    filming=true; 
    frameCounter=0;
  }
  if (key == 'R') { 
    recording=!recording;
  } // pause filming on/off: saves frames as .tif images in /animation folder
  if (key == 'H') { 
    scribeText=!scribeText;
  } // pause filming on/off
  if (key == 'P') { 
    if (startedFilm) filming=!filming;
  } // pause filming on/off
  if (key == 'I') {  
    myFace = loadImage("anim/frame"+nf(kf++, 2)+".jpg");
  }
  if (key == 'B') { 
    kf--; 
    myFace = loadImage("anim/frame"+nf(kf, 2)+".jpg");
  }

  MYkeyPressed();
}
int kf=1;



void stop() //songStuff
{
  song.close(); //songStuff
  minim.stop();//songStuff
 
  super.stop();//songStuff
}             //songStuff

/* 
 To snap a picture of the window, press 'X'.
 IT will be saved as a Pnnn.jpg file in the PICTURES folder
 
 To make a movie that captures only frames when a key is pressed or released or when the mouse is pressed, moved, or dragged
 or when you are capturing an animaiton, press 
 'S' to start filming a new movie
 'P' to pause so that you can do things that you don't want to film
 'P' to restart filming
 'p' to play and film one run of your animation
 'E' to end the current film 
 These films will me saved in the folder FILMS in your sketch folder
 
 To make a movie capturing all the frames as if in realtime: 
 Press 'R' to toggle Recording on/off
 While filming, each frame si captured as a .tif image in the folder /FRAMES
 On a MAC, go to QuickTimePlayer
 File > Open Image Sequence
 Locate your sketch folder and the animation folder, sort it by name
 Double click on the first .tif file
 Selet frame rate (24 fps is OK), but if you want realtime, select 60 fps
 Play your animation to check it
 Stop at a good frame for creating the pister image
 File > Export for web
 Select Desktop and crate poster image from current frame
 provide a name XXX and select the place where to store your video
 in the XXX folder, your movie is the .m43 file
 the ReadMe.html file imbeds the video and provides html code for including it in your own project web page. Do that.
 Copy the folder to where your project web page is.
 Delete the animation folder, since we don't need these .tif files any more
 
 To place some text in the window:
 cut the text into the clipboard
 click in the window of the sketch
 press 'T' and drrag the mouse to place your text
 release (first the 't' key) to leave it there 
 To change the text, redo the above steps
 
 The tools for pasting text, for displaying cursor and key actions, and for saving image and videos are in the media tab  
 
 
 */
